#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class Web_ModuleProvider : CY.GFive.Core.Providers.DALProvider.IWeb_ModuleProvider
	{
		#region Sql
		private static readonly string SqlSelectWeb_Module = "SELECT [Id], [Module_Code],[Module_Name],[Module_IsParent] FROM [Web_Module] WHERE  [Id]=@Id";

		private static readonly string SqlInsertWeb_Module = "INSERT INTO [Web_Module]([Module_Code],[Module_Name],[Module_IsParent]) values(@Module_Code,@Module_Name,@Module_IsParent);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateWeb_Module = "UPDATE [Web_Module] SET [Module_Code] = @Module_Code,[Module_Name] = @Module_Name,[Module_IsParent] = @Module_IsParent WHERE  [Id]=@Id";

		private static readonly string SqlDeleteWeb_Module = "DELETE [Web_Module] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllWeb_Module = "SELECT [Id], [Module_Code],[Module_Name],[Module_IsParent] FROM [Web_Module]";
		#endregion

		#region IWeb_ModuleProvider Members
		public Core.Business.Web_Module Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectWeb_Module);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Web_Module web_Module = new Core.Business.Web_Module();

				if (!reader.IsDBNull(0)) web_Module.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) web_Module.Module_Code = reader.GetString(1);
				if (!reader.IsDBNull(2)) web_Module.Module_Name = reader.GetString(2);
				if (!reader.IsDBNull(3)) web_Module.Module_IsParent = reader.GetInt32(3);

				return web_Module;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Web_Module web_Module)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Module_Code", SqlDbType.NVarChar, web_Module.Module_Code);
			sql.AddParameter("@Module_Name", SqlDbType.NVarChar, web_Module.Module_Name);
			sql.AddParameter("@Module_IsParent", SqlDbType.Int, web_Module.Module_IsParent);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertWeb_Module);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) web_Module.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Web_Module web_Module)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Module_Code", SqlDbType.NVarChar, web_Module.Module_Code);
			sql.AddParameter("@Module_Name", SqlDbType.NVarChar, web_Module.Module_Name);
			sql.AddParameter("@Module_IsParent", SqlDbType.Int, web_Module.Module_IsParent);
			sql.AddParameter("@Id", SqlDbType.Int, web_Module.Id);
			sql.ExecuteSql(SqlUpdateWeb_Module);
		}

		public void Delete(Core.Business.Web_Module web_Module)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, web_Module.Id);
			sql.ExecuteSql(SqlDeleteWeb_Module);
		}

		public IList<Core.Business.Web_Module> GetAllWeb_Module()
		{
			IList<Core.Business.Web_Module> web_Modulelist = new List<Core.Business.Web_Module>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllWeb_Module);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Web_Module web_Module = new Core.Business.Web_Module();

					if (!reader.IsDBNull(0)) web_Module.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) web_Module.Module_Code = reader.GetString(1);
					if (!reader.IsDBNull(2)) web_Module.Module_Name = reader.GetString(2);
					if (!reader.IsDBNull(3)) web_Module.Module_IsParent = reader.GetInt32(3);

					web_Module.MarkOld();
					web_Modulelist.Add(web_Module);
				}
				reader.Close();
			}
			return web_Modulelist;
		}
		#endregion
	}
}
